#include "UIComboBox.h"
#include "win_gdi.h"

UIComboBox::UIComboBox(UIContainer *parent) : UIContainer(parent)
{
    setName("UIComboBox");
    setBorderWidth(1);
    setPadding(DuiCorner{1, 1, 1, 4});
    setTextAlignX(DUI_TEXT_ALIGN_LEFT);

    m_Items.push_back(L"item1");
    m_Items.push_back(L"item2");
    m_Items.push_back(L"item3");
    m_Items.push_back(L"item4");
    m_SelectedIndex = 0;
}

UIComboBox::~UIComboBox()
{
}

std::string UIComboBox::getUIType()
{
    return "UIComboBox";
}

bool UIComboBox::isInContainer(int x, int y)
{
    if (m_IsDropDown)
    {
        auto w = m_Rect.w;
        auto h = m_Rect.h + m_ExtendHeight;
        return x > m_Rect.x && x < m_Rect.x + w && y > m_Rect.y && y < m_Rect.y + h;
    }
    return UIContainer::isInContainer(x, y);
}

void UIComboBox::onMouseDown(int x, int y)
{
    // dropdown click
    if (UIContainer::isInContainer(x, y))
    {
        m_IsDropDown = !m_IsDropDown;
        setNeedUpdate(true);
    }
    //  下拉框内容点击
    if (m_IsDropDown)
    {
        auto h = m_ExtendHeight / m_Items.size();
        auto y2 = m_Rect.y + m_Rect.h + m_ExtendHeight;
        for (int i = 0; i < m_Items.size(); i++)
        {
            auto y3 = m_Rect.y + m_Rect.h + i * h;
            if (y > y3 && y < y3 + h)
            {
                m_SelectedIndex = i;
                setIsDropDown(false);
                setNeedUpdate(true);
            }
        }
    }
}

void UIComboBox::onMouseLeave()
{
    UIContainer::onMouseLeave();
    setIsDropDown(false);
}

void UIComboBox::setTextW(const std::wstring text)
{
}

std::wstring UIComboBox::getTextW()
{
    std::wstring text;
    if (m_SelectedIndex >= 0 && m_SelectedIndex < m_Items.size())
    {
        text = m_Items[m_SelectedIndex];
    }
    else
    {
        text = L"";
    }
    return text;
}

void UIComboBox::onDrawContent(DuiWinManager *mgr, DuiRect &rect_in, DuiRect &rect_out)
{
    // 文本
    auto text = getTextW();
    if (text.length() > 0)
    {
        render_draw_text(mgr, rect_in, text, m_Color, m_TextAlignX, m_TextAlignY, m_FontFamily, m_FontSize,
                         m_FontWeight);
    }

    // 下拉框
    DuiRect rect_dropdown;
    rect_dropdown.x = rect_in.x + rect_in.w - rect_in.h;
    rect_dropdown.y = rect_in.y;
    rect_dropdown.w = rect_in.h;
    rect_dropdown.h = rect_in.h;
    render_draw_fill(mgr, rect_dropdown, DuiColor{0, 255, 255, 255}, m_BorderRadius);

    // 下拉框内容
    if (m_IsDropDown)
    {
        DuiRect rect_dropdown_content;
        rect_dropdown_content.x = m_Rect.x;
        rect_dropdown_content.y = m_Rect.y + m_Rect.h;
        rect_dropdown_content.w = m_Rect.w;
        rect_dropdown_content.h = m_ExtendHeight;
        render_draw_fill(mgr, rect_dropdown_content, m_BackgroundColor, 0);
        for (int i = 0; i < m_Items.size(); i++)
        {
            auto item = m_Items[i];
            auto rc =
                DuiRect{rect_dropdown_content.x, rect_dropdown_content.y + i * m_ExtendHeight / int(m_Items.size()),
                        rect_dropdown_content.w, rect_dropdown_content.h / int(m_Items.size())};
            if (m_SelectedIndex == i)
            {
                render_draw_fill(mgr, rc, DuiColor{0, 255, 255, 120}, 0);
            }
            auto mousePos = getMousePos();
            if (rc.x < mousePos.x && mousePos.x < rc.x + rc.w && rc.y < mousePos.y && mousePos.y < rc.y + rc.h)
            {
                render_draw_fill(mgr, rc, DuiColor{0, 255, 0, 120}, 0);
            }
            rc.x += 4;
            rc.w -= 8;
            render_draw_text(mgr, rc, item, m_Color, DUI_TEXT_ALIGN_LEFT, DUI_TEXT_ALIGN_CENTER,
                             m_FontFamily, m_FontSize, m_FontWeight);
        }
        render_draw_rect(mgr, rect_dropdown_content, m_BorderColor, 1, DUI_LINE_STYLE_SOLID, 0);
    }
}
